package com.study.spark.scala.redis

import org.apache.spark.sql.{SaveMode, SparkSession}

/**
 *
 * @author stephen
 * @date 2019-10-10 16:51
 */
object RedisDemo {

  case class Person(name: String, age: Int)

  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName("redis-demo")
      .master("local[*]")
      .config("spark.redis.host", "localhost")
      .config("spark.redis.port", "6379")
      //      .config("spark.redis.auth","redispwd")  //用户权限配置
      //      .config("spark.redis.db","8")  //数据库设置
      //      .config("spark.redis.timeout","2000")  //设置连接超时时间
      .getOrCreate()

    val personSeq = Seq(Person("John", 30), Person("Peter", 45))
    val df = spark.createDataFrame(personSeq)

    df.write
      .format("org.apache.spark.sql.redis")
      .option("table", "person")
      //.option("key.column", "name") // hgetall person:John
      .mode(SaveMode.Overwrite)
      .save()

    val loadedDf = spark.read
      .format("org.apache.spark.sql.redis")
      .option("table", "person")
      .load()

    loadedDf.printSchema()
    loadedDf.show()
  }
}
